<?php
/**
 * Created by jinqiang on 2024/6/5 14:44.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Banshi;


use App\Http\Models\Mysql\SsyNewProductPositionTable;

class PositionService
{
    public function search($param)
    {
        $conn = (new SsyNewProductPositionTable())->where("position_status", (Int)$param['status']);
        if(isset($param['title']) && !empty($param['title']) && $param['title']!="ALL"){
            if($param['searchType'] ?? ""){//是否精确搜索
                $conn = $conn->where('position_title', $param['title']);
            }else{
                $conn = $conn->where('position_title', 'like', "%".$param['title']."%");
            }
        }
        if($param['ids'] ?? []){
            $conn = $conn->whereIn('id', $param['ids']);
        }
        $res = $conn->orderBy("createtime", "desc")->paginate((Int)$param['limit'])->toArray();
        return ['data' => $res['data'], 'total' => $res['total']];
    }

    public function save($id, $data)
    {
        if(isset($data['position_title'])){
            $exist = (new SsyNewProductPositionTable())->where('position_title', $data['position_title'])->exists();
            if($exist){
                return ['code' => -1, 'msg' => '部位名称已存在'];
            }
        }
        return (new SsyNewProductPositionTable())->updateData($id, $data);
    }

    public function create($data)
    {
        $exist = (new SsyNewProductPositionTable())->where('position_title', $data['position_title'])->exists();
        if($exist){
            return ['code' => -1, 'msg' => '部位名称已存在'];
        }
        return (new SsyNewProductPositionTable())->createData($data);
    }
}
